<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Contracts\Validation\UnauthorizedException;
use Illuminate\Support\Facades\Auth;
use Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException;

class BusinessAuth
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request $request
     * @param  \Closure                 $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        foreach (['business'] as $driver) {
            if (Auth::guard($driver)->check()) {
                Auth::setDefaultDriver($driver);
                break;
            }
        }

        if (Auth::guest()) {
            info('mid401', [$request->all(), $request->getRequestUri()]);
            throw new UnauthorizedHttpException('401', '认证失败, 请先登录');
        }
        return $next($request);
    }
}
